﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MixScript.BLL.FI;
using System.Data;

namespace MixScriptWeb.Accounting.ReportObject
{
    public partial class rpt_GeneralLedger : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblDate.Text = DateTime.Now.ToLongDateString();
            if (Session["ledgerParam"] != null)
            {
                DataSet ds = new DataSet();
                GridView gv;
                String param = (String)Session["ledgerParam"];
                DateTime from = (DateTime)Session["fromParam"];
                DateTime to = (DateTime)Session["toParam"];
                lblPeriod.Text = "Period : " + from.ToShortDateString() +
                    " S/D " + to.ToShortDateString();
                Report rpt = new Report();
                ds = rpt.GeneralJournal(param, from, to);

                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    Report rpt2 = new Report();
                    //ds.Tables[i].TableName
                    ViewState["BeginingBalance"] = 0;
                    Literal ltrDyn = new Literal();
                    ltrDyn.Text = String.Format("<br /> {0} <br />", ds.Tables[i].TableName);
                    Literal ltrDyn1 = new Literal();
                    ltrDyn1.Text = String.Format("<br /> <br />", ds.Tables);
                    gv = new GridView();
                    gv.Width = pnlGrid.Width;
                    gv.RowDataBound += new GridViewRowEventHandler(gvList_RowdataBond);
                    gv.DataSource = ds.Tables[i];
                    gv.DataBind();
                    gv.CssClass = "GridView";

                    //gv.AlternatingRowStyle = "AlternatingRowStyle";
                    pnlGrid.Controls.Add(ltrDyn);
                    pnlGrid.Controls.Add(gv);
                    pnlGrid.Controls.Add(ltrDyn1);

                }
                Session["ledgerParam"] = null;
                Session["fromParam"] = null;
                Session["toParam"] = null;
            }
        }
        protected void gvList_RowdataBond(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                GridView gv = (GridView)sender;
                GridViewRow rowAsli = e.Row;
                GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);

                TableCell cell = new TableHeaderCell();
                cell.Text = "Last Balance ...";
                cell.Style["valign"] = "left";
                cell.ColumnSpan = 10;
                row.Cells.Add(cell);

                TableCell cell2 = new TableHeaderCell();
                cell2.Text = ViewState["BeginingBalance"].ToString();
                cell2.Style["valign"] = "right";
                cell2.ColumnSpan = 1;
                row.Cells.Add(cell2);


                Table t = (Table)gv.Controls[0];
                if (t != null)
                    t.Rows.AddAt(1, row);

            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (ViewState["buff"] == null)
                    ViewState["buff"] = e.Row.Cells[0].Text;
                else
                    if ((String)ViewState["buff"] == e.Row.Cells[0].Text)
                        e.Row.Cells[0].Text = "";

                Decimal Neraca = Convert.ToDecimal(ViewState["BeginingBalance"]);
                Decimal Debit = Convert.ToDecimal(e.Row.Cells[8].Text.Replace("&nbsp;", "0"));
                Decimal Kredit = Convert.ToDecimal(e.Row.Cells[9].Text.Replace("&nbsp;", "0"));
                e.Row.Cells[10].Text = (Neraca + Debit - Kredit).ToString(); ;

                if (ViewState["_Debit"] == null)
                    ViewState["_Debit"] = Convert.ToDecimal(e.Row.Cells[8].Text.Replace("&nbsp;", "0"));
                else
                    ViewState["_Debit"] = (Decimal)ViewState["_Debit"] + Convert.ToDecimal(e.Row.Cells[8].Text.Replace("&nbsp;", "0"));
                if (ViewState["_Kredit"] == null)
                    ViewState["_Kredit"] = Convert.ToDecimal(e.Row.Cells[9].Text.Replace("&nbsp;", "0"));
                else
                    ViewState["_Kredit"] = (Decimal)ViewState["_Kredit"] + Convert.ToDecimal(e.Row.Cells[9].Text.Replace("&nbsp;", "0"));
                if (ViewState["_Neraca"] == null)
                    ViewState["_Neraca"] = Convert.ToDecimal(e.Row.Cells[10].Text.Replace("&nbsp;", "0"));
                else
                    ViewState["_Neraca"] = (Decimal)ViewState["_Neraca"] + Convert.ToDecimal(e.Row.Cells[10].Text.Replace("&nbsp;", "0"));

                e.Row.Cells[6].Text = String.Format("{0:##,##.00}", Convert.ToDecimal(e.Row.Cells[6].Text.Replace("&nbsp;", "0")));
                e.Row.Cells[8].Text = String.Format("{0:##,##.00}", Convert.ToDecimal(e.Row.Cells[8].Text.Replace("&nbsp;", "0")));
                e.Row.Cells[9].Text = String.Format("{0:##,##.00}", Convert.ToDecimal(e.Row.Cells[9].Text.Replace("&nbsp;", "0")));
                e.Row.Cells[10].Text = String.Format("{0:##,##.00}", Convert.ToDecimal(e.Row.Cells[10].Text.Replace("&nbsp;", "0")));

            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                GridView gv = (GridView)sender;
                GridViewRow rowAsli = e.Row;
                GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);

                TableCell cell = new TableHeaderCell();
                cell.Text = "Total ...";
                cell.Style["valign"] = "left";
                cell.ColumnSpan = 8;
                row.Cells.Add(cell);

                TableCell cell2 = new TableHeaderCell();
                cell2.Text = String.Format("{0:##,##.00}", (Decimal)ViewState["_Debit"]);
                cell2.Style["valign"] = "right";
                cell2.ColumnSpan = 1;
                row.Cells.Add(cell2);

                TableCell cell3 = new TableHeaderCell();
                cell3.Text = String.Format("{0:##,##.00}", (Decimal)ViewState["_Kredit"]);
                cell3.Style["valign"] = "right";
                cell3.ColumnSpan = 1;
                row.Cells.Add(cell3);

                TableCell cell4 = new TableHeaderCell();
                cell4.Text = String.Format("{0:##,##.00}", (Decimal)ViewState["_Neraca"]);
                cell4.Style["valign"] = "right";
                cell4.ColumnSpan = 1;
                row.Cells.Add(cell4);


                Table t = (Table)gv.Controls[0];
                if (t != null)
                    t.Rows.Add(row);
            }


        }
    }
}